package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.ClueDialogTitleAdp;
import com.baihan.angus.app.game.busshow.adps.RoleScriptAdp;
import com.baihan.angus.app.game.common.bean.response.DmRoleScriptResponse;

import java.util.List;
import java.util.stream.Collectors;

import static com.baihan.angus.app.game.busshow.pop_dialog.RoleScriptDetailDialog.ROLE_SCRIPT_MENU_CHECK;

public class DmRoleScriptDialog extends Dialog {

    private Context mContext;
    private Callback mCallback;
    private WindowManager.LayoutParams mLayoutParams;

    private TextView tvTitle;
    private ImageView ivClose;
    private RecyclerView rvScriptTitle, rvScript;
    private LinearLayout llNoData;

    private RoleScriptAdp mRoleScriptAdp;
    private ClueDialogTitleAdp mClueDialogTitleAdp;
    private List<String> scriptList;
    private List<String> titleList;
    private DmRoleScriptResponse.DataXX roleScriptData;

    public DmRoleScriptDialog(@NonNull Context context, Callback mCallback) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        this.mCallback = mCallback;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = (int) (AppUtil.getScreenHeight(context) * 0.8);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        dialogWindow.getDecorView().setBackgroundResource(android.R.color.transparent);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_dm_role_script, (ViewGroup) null);
        setContentView(dialogView);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        rvScriptTitle = dialogView.findViewById(R.id.rvScriptTitle);
        ivClose = dialogView.findViewById(R.id.ivClose);
        rvScript = dialogView.findViewById(R.id.rvScript);
        llNoData = dialogView.findViewById(R.id.llNoData);

        mClueDialogTitleAdp = new ClueDialogTitleAdp();
        rvScriptTitle.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mClueDialogTitleAdp.bindToRecycleView(rvScriptTitle);

        mClueDialogTitleAdp.setOnItemClickListener(new ClueDialogTitleAdp.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                mClueDialogTitleAdp.setThisPosition(position);
                mClueDialogTitleAdp.notifyDataSetChanged();
                mRoleScriptAdp.clear();
                if (null != scriptList) {
//                    mScriptAdp.addAll(clueList.get(position).getData());
                    rvScript.setVisibility(View.VISIBLE);
                    llNoData.setVisibility(View.GONE);
                } else {
                    rvScript.setVisibility(View.GONE);
                    llNoData.setVisibility(View.VISIBLE);
                }
            }
        });

        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        mRoleScriptAdp = new RoleScriptAdp();
        rvScript.setLayoutManager(gridLayoutManager);
        rvScript.setAdapter(mRoleScriptAdp);

        mRoleScriptAdp.setOnItemClickListener(new RoleScriptAdp.OnItemClickListener() {
            @Override
            public void onView(int position) {
                if (null != mCallback) {
                    //切换item先将menu位置设为0
                    ROLE_SCRIPT_MENU_CHECK = 0;
                    mCallback.onShowDetail(roleScriptData.getScript_data().get(0).getData().get(position).getData());
                }
            }
        });

        ivClose.setOnClickListener(v -> {
            cancel();
        });
    }

    public void initData(DmRoleScriptResponse.DataXX dataXX) {
        roleScriptData = dataXX;
        mClueDialogTitleAdp.clear();
        mRoleScriptAdp.clear();
        //该方法6.0不兼容
        //总的底部目录
        titleList = dataXX.getScript_data().stream().map(DmRoleScriptResponse.DataXX.ScriptData::getTitle).collect(Collectors.toList());
        mClueDialogTitleAdp.addAll(titleList);
        mRoleScriptAdp.addAll(dataXX.getScript_data().get(0).getData());
    }

    public interface Callback {
        void onShowDetail(List<DmRoleScriptResponse.DataXX.ScriptData.DataX.Data> data);
    }
}
